package com.pig4cloud.pig.userinfo.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.security.annotation.Inner;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import com.pig4cloud.plugin.excel.annotation.RequestExcel;
import com.pig4cloud.pig.userinfo.entity.UserinfoEntity;
import com.pig4cloud.pig.userinfo.service.UserinfoService;

import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import com.pig4cloud.pig.common.security.annotation.HasPermission;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 用户信息表
 *
 * @author pig
 * @date 2025-04-17 11:51:08
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/userinfo" )
@Tag(description = "userinfo" , name = "用户信息表管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class UserinfoController {

    private final  UserinfoService userinfoService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param userinfo 用户信息表
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @HasPermission("userinfo_userinfo_view")
    public R getUserinfoPage(@ParameterObject Page page, @ParameterObject UserinfoEntity userinfo) {
        LambdaQueryWrapper<UserinfoEntity> wrapper = Wrappers.lambdaQuery();
        return R.ok(userinfoService.page(page, wrapper));
    }


    /**
     * 通过条件查询用户信息表
     * @param userinfo 查询条件
     * @return R  对象列表
     */
    @Operation(summary = "通过条件查询" , description = "通过条件查询对象" )
    @GetMapping("/details" )
    @HasPermission("userinfo_userinfo_view")
    public R getDetails(@ParameterObject UserinfoEntity userinfo) {
        return R.ok(userinfoService.list(Wrappers.query(userinfo)));
    }

    /**
     * 新增用户信息表
     * @param userinfo 用户信息表
     * @return R
     */
    @Operation(summary = "新增用户信息表" , description = "新增用户信息表" )
    @SysLog("新增用户信息表" )
    @PostMapping
    @HasPermission("userinfo_userinfo_add")
    public R save(@RequestBody UserinfoEntity userinfo) {
        return R.ok(userinfoService.save(userinfo));
    }

    /**
     * 修改用户信息表
     * @param userinfo 用户信息表
     * @return R
     */
    @Operation(summary = "修改用户信息表" , description = "修改用户信息表" )
    @SysLog("修改用户信息表" )
    @PutMapping
    @HasPermission("userinfo_userinfo_edit")
    public R updateById(@RequestBody UserinfoEntity userinfo) {
        return R.ok(userinfoService.updateById(userinfo));
    }

    /**
     * 通过id删除用户信息表
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除用户信息表" , description = "通过id删除用户信息表" )
    @SysLog("通过id删除用户信息表" )
    @DeleteMapping
    @HasPermission("userinfo_userinfo_del")
    public R removeById(@RequestBody Integer[] ids) {
        return R.ok(userinfoService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param userinfo 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @HasPermission("userinfo_userinfo_export")
    public List<UserinfoEntity> exportExcel(UserinfoEntity userinfo,Integer[] ids) {
        return userinfoService.list(Wrappers.lambdaQuery(userinfo).in(ArrayUtil.isNotEmpty(ids), UserinfoEntity::getId, ids));
    }

    /**
     * 导入excel 表
     * @param userinfoList 对象实体列表
     * @param bindingResult 错误信息列表
     * @return ok fail
     */
    @PostMapping("/import")
    @HasPermission("userinfo_userinfo_export")
    public R importExcel(@RequestExcel List<UserinfoEntity> userinfoList, BindingResult bindingResult) {
        return R.ok(userinfoService.saveBatch(userinfoList));
    }

    /**
     * 通过手机号查询用户信息
     * @param phone 手机号码
     * @return R 用户信息
     */
    @Operation(summary = "通过手机号查询", description = "通过手机号查询用户信息")
    @GetMapping("/phone/{phone}")
//	@Inner(value = false)
    @HasPermission("userinfo_userinfo_view")  // 可以暂时注释权限控制，方便测试
    public R getByPhone(@PathVariable String phone) {
        LambdaQueryWrapper<UserinfoEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(UserinfoEntity::getPhone, phone);
        return R.ok(userinfoService.getOne(wrapper));
    }
    
    /**
     * 舌象辨识分析
     * @param imageBase64 舌象图片的Base64编码
     * @return R 辨识结果
     */
    @Operation(summary = "舌象辨识分析", description = "通过舌象图片进行体质辨识")
    @PostMapping("/tongue-analysis")
    @Inner(value = false)
    public R tongueAnalysis(@RequestBody String imageBase64) {
        // 简化实现，随机返回一种体质类型
        String[] constitutionTypes = {
            "平和质", "气虚质", "阳虚质", "阴虚质", "痰湿质", 
            "湿热质", "血瘀质", "气郁质", "特禀质"
        };
        
        // 随机选择一种体质类型
        int randomIndex = (int) (Math.random() * constitutionTypes.length);
        String result = constitutionTypes[randomIndex];
        
        // 返回辨识结果
        return R.ok(result);
    }
}
